package com.example.sunmiv1.adapter;

import android.content.Context;
//import android.support.v4.content.ContextCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.member.MemberGoodsClassInfo;
import com.example.sunmiv1.entity.member.MemberLabelManagerInfo;

import java.util.List;

public class MemberTimeAdapter extends RecyclerView.Adapter<MemberTimeAdapter.ViewHolder> {

    private List<?> list;
    private int index = 0;
    private Context context;

    public MemberTimeAdapter(List<?> list) {
        this.list = list;
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_time, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (list.get(position) instanceof String) {
            holder.memberTimeTv.setText((String) list.get(position));
        } else if (list.get(position) instanceof MemberGoodsClassInfo.DataBean.ListBean) {
            holder.memberTimeTv.setText(((MemberGoodsClassInfo.DataBean.ListBean)list.get(position)).getClass_name());
        }else if(list.get(position) instanceof MemberLabelManagerInfo.DataBean.ListBean){
            holder.memberTimeTv.setText(((MemberLabelManagerInfo.DataBean.ListBean)list.get(position)).getName());
        }
        if (index == position) {
            holder.memberTimeTv.setBackgroundResource(R.color.login_textColor1);
            holder.memberTimeTv.setTextColor(ContextCompat.getColor(context, R.color.white));
        } else {
            holder.memberTimeTv.setBackgroundResource(R.drawable.line_border);
            holder.memberTimeTv.setTextColor(ContextCompat.getColor(context, R.color.text_333));
        }
        holder.memberTimeTv.setOnClickListener(v -> click.success(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView memberTimeTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            memberTimeTv = itemView.findViewById(R.id.memberTime_tv);
        }
    }

    private Click click;

    public void Choose(Click click) {
        this.click = click;
    }

    public interface Click {
        void success(int index);
    }
}
